<?php

/**
 * This is the model class for table "{{payment}}".
 *
 * The followings are the available columns in table '{{payment}}':
 * @property integer $zpy_id
 * @property string $zpy_name
 * @property string $zpy_content
 * @property string $zpy_config
 * @property integer $zpy_available
 * @property integer $zpy_online
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Payment extends CActiveRecord
{
	//接口类名称
	public $class_name;
	//账号
	public $zpy_payment_id;
	//用户名
	public $zpy_payment_user;
	//密匙
	public $zpy_payment_key;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Payment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{payment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zpy_name', 'required'),
			array('zpy_available, zpy_online, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zpy_name', 'length', 'max'=>25),
			array('class_name,zpy_payment_id,zpy_payment_user,zpy_payment_key', 'length', 'max'=>100),
			array('zpy_content, zpy_config', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpy_id, zpy_name, zpy_content, zpy_config, zpy_available, zpy_online, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpy_id' => 'Zpy',
			'zpy_name' => 'Zpy Name',
			'zpy_content' => 'Zpy Content',
			'zpy_config' => 'Zpy Config',
			'zpy_available' => 'Zpy Available',
			'zpy_online' => 'Zpy Online',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpy_id',$this->zpy_id);
		$criteria->compare('zpy_name',$this->zpy_name,true);
		$criteria->compare('zpy_content',$this->zpy_content,true);
		$criteria->compare('zpy_config',$this->zpy_config,true);
		$criteria->compare('zpy_available',$this->zpy_available);
		$criteria->compare('zpy_online',$this->zpy_online);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 启用状态
	 */
	public function getAvailable($type=null){
		if($type==null){
			return array(
				'1'=>'停用',
				'2'=>'启用',
			);
		}else{
			$level = $this->getAvailable();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 启用状态
	 */
	public function getZpyOnline($type=null){
		if($type==null){
			return array(
					'1'=>'线下支付',
					'2'=>'线上支付',
			);
		}else{
			$level = $this->getZpyOnline();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 保存前的操作 将本配置信息系列化保存
	 */
	protected function beforeSave(){
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
			}else{
				$this->updatetime = time();
			}
			$this->zpy_config = serialize(array(
					'class_name'=>$this->class_name,
					'zpy_payment_user'=>$this->zpy_payment_user,
					'zpy_payment_id'=>$this->zpy_payment_id,
					'zpy_payment_key'=>$this->zpy_payment_key,
			));
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 读取数据后的操作，将配置信息取出并反系列化
	 */
	protected function afterFind(){
		$payment = unserialize($this->zpy_config);
		$this->class_name = $payment['class_name'];
		$this->zpy_payment_user = $payment['zpy_payment_user'];
		$this->zpy_payment_id = $payment['zpy_payment_id'];
		$this->zpy_payment_key = $payment['zpy_payment_key'];
		$this->zpy_config = $payment;
		return true;
	}
	
	/**
	 * 支付
	 * @param $zpy_id 支付方式ID
	 * @param $zm_id 会员ID
	 * @param $order_id 订单ID
	 * @param $amount 金额
	 */
	public function pay($param,$zpy_id = 4){
		//支付方式
		$payment = $this->findByPk($zpy_id);
		//实例化支付类
		$alipay = new $payment->class_name();
		
		//配置
		$alipay->pay_config = $payment->zpy_config;
		//订单详细
		// $params = array(
		// 		"out_trade_no"	=> '',//商户订单号
		// 		"subject"	=> '',//订单名称
		// 		"price"	=> '',//付款金额
		// 		"quantity"	=> '1',//商品数量
		// 		"logistics_fee"	=> '0',//物流费用
		// 		"logistics_type"	=> 'EXPRESS',//物流类型，三个值可选：EXPRESS（快递）、POST（平邮）、EMS（EMS）
		// 		"logistics_payment"	=> 'SELLER_PAY',//物流支付方式SELLER_PAY（卖家承担运费）、BUYER_PAY（买家承担运费）
		// 		"body"	=> '', //订单描述
		// 		"show_url"	=> 'http://www.girlxy.com/product/id/252',//商品展示地址
		// 		"receive_name"	=> '胡天',//收货人姓名
		// 		"receive_address" => '湖南省岳阳中心小学',//收货人地址
		// 		"receive_zip"	=> '521410', //收货人邮编
		// 		"receive_phone"	=> '0784-2564815',//收货人电话号码
		// 		"receive_mobile"	=> '13586459751',//收货人手机号码
		// );
		// $params = array_merge($params, $param);
		//对数组按照键名排序，保留键名到数据的关联。
		//ksort($params);
		//将数组的内部指针指向第一个单元
		//reset($params);
		return $alipay->buildForm($param);
	}
}